/**
 * @title FilterUtil.java
 * @package com.esun.cheetah.biz.match.matchlist
 * @description 
 * @author liugan
 * @date 2012-4-5
 * @copyright 深圳市易讯天空
 */
package com.esun.cheetah.biz.match.matchlist;

import java.util.ArrayList;
import java.util.HashSet;

import com.esun.cheetah.model.match.matchlist.AbsMatchListSubItemInfo;
import com.esun.cheetah.model.match.matchlist.MatchListPeilvSubItemInfo;
import com.esun.cheetah.model.match.matchlist.MatchListScoreSubItemInfo;
import com.esun.util.log.EDebug;

/**
 * @title FilterUtil
 * @description 用于过滤比赛
 * @author liugan
 */
public class FilterUtil {

	/**
	 * 比赛的状态
	 */
	public static final int STATE_ALL = 0;
	public static final int STATE_RIGHT_NOW = AbsMatchListSubItemInfo.SHOW_STATE_RIGHT_NOW;
	public static final int STATE_FUTURE = AbsMatchListSubItemInfo.SHOW_STATE_FUTURE;
	public static final int STATE_DONE = AbsMatchListSubItemInfo.SHOW_STATE_DONE;

	private static FilterUtil mInstance;
	private boolean needFilter;

	private boolean isToday;

	public boolean isToday() {
		return isToday;
	}

	public void setToday(boolean isToday) {
		this.isToday = isToday;
	}

	private ArrayList<? extends AbsMatchListSubItemInfo> mMatchList;

	public void setMatchList(
			ArrayList<? extends AbsMatchListSubItemInfo> matchList) {
		this.mMatchList = matchList;
	}

	private HashSet<String> leagueSet;
	private HashSet<String> selectedLeagueSet = new HashSet<String>();

	public void setSelectedLeagueSet(HashSet<String> selectedLeagueSet) {
		EDebug.o("" + selectedLeagueSet);
		this.selectedLeagueSet.clear();
		this.selectedLeagueSet.addAll(selectedLeagueSet);
	}

	public ArrayList<? extends AbsMatchListSubItemInfo> getMatchList() {
		return mMatchList;
	}

	public void setState(int state) {
		this.state = state;
	}

	private int state;

	private FilterUtil() {
	}

	public static synchronized FilterUtil getInstance() {
		if (mInstance == null) {
			mInstance = new FilterUtil();
		}
		return mInstance;
	}

	public boolean isNeedFilter() {
		return needFilter;
	}

	public void setNeedFilter(boolean needFilter) {
		if (!needFilter) {
			selectedLeagueSet.clear();
		}
		this.needFilter = needFilter;
	}

	public HashSet<String> getLeagueSet() {
		return leagueSet;
	}

	public void setLeagueSet(HashSet<String> leagueSet) {
		this.leagueSet = leagueSet;
	}

	public int getState() {
		return state;
	}

	public String[] getAllLeagues() {
		HashSet<String> leagueNames = new HashSet<String>();
		for (AbsMatchListSubItemInfo info : mMatchList) {
			if (state == FilterUtil.STATE_ALL
					|| info.getShowState() == this.state) {
				leagueNames.add(info.getLeagueName());
			}
		}
		String[] names = new String[leagueNames.size()];
		leagueNames.toArray(names);
		EDebug.o("leagueNames : " + names.toString());
		return names;
	}

	public String[] getAllLeaguesAndChangeSelected(int state) {
		selectedLeagueSet.clear();
		HashSet<String> leagueNames = new HashSet<String>();
		for (AbsMatchListSubItemInfo info : mMatchList) {
			if (state == FilterUtil.STATE_ALL || info.getShowState() == state) {
				leagueNames.add(info.getLeagueName());
				selectedLeagueSet.add(info.getLeagueName());
			}
		}
		String[] names = new String[leagueNames.size()];
		return leagueNames.toArray(names);
	}

	public HashSet<String> getSelectedLeagueSet() {

		return selectedLeagueSet;
	}

	/**
	 * @title doFilter
	 * @description 过滤，将不合条件的项删除
	 * @param matchList
	 */
	public ArrayList<MatchListPeilvSubItemInfo> doFilter4Peilv(
			ArrayList<MatchListPeilvSubItemInfo> matchList) {
		ArrayList<MatchListPeilvSubItemInfo> resultList = new ArrayList<MatchListPeilvSubItemInfo>();
		for (int i = 0; i < matchList.size(); i++) {
			MatchListPeilvSubItemInfo info = matchList.get(i);
			// 联赛名称和状态都匹配
			if ((state == STATE_ALL || state == info.getShowState())
					&& selectedLeagueSet.contains(info.getLeagueName())) {
				resultList.add(matchList.get(i));
			}
		}
		return resultList;
	}

	/**
	 * @title doFilter4Score
	 * @description
	 * @param list
	 * @return
	 */
	public ArrayList<MatchListScoreSubItemInfo> doFilter4Score(
			ArrayList<MatchListScoreSubItemInfo> matchList) {
		ArrayList<MatchListScoreSubItemInfo> resultList = new ArrayList<MatchListScoreSubItemInfo>();
		for (int i = 0; i < matchList.size(); i++) {
			MatchListScoreSubItemInfo info = matchList.get(i);
			// 联赛名称和状态都匹配
			if ((state == STATE_ALL || state == info.getShowState())
					&& selectedLeagueSet.contains(info.getLeagueName())) {
				resultList.add(matchList.get(i));
			}
		}
		return resultList;
	}

}
